package com.kma.summer2012.client.view.manager;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.MultiSelectionModel;
import com.kma.summer2012.client.constants.ConstantsPresenter;
import com.kma.summer2012.client.presenters.manager.ManageToolsPresenter;
import com.kma.summer2012.client.styles.style2.Recources;
import com.kma.summer2012.shared.model.UICrumbsToolItem;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.Set;

/**
 * Created with IntelliJ IDEA.
 * User: Администратор
 * Date: 22.02.13
 * Time: 0:04
 * To change this template use File | Settings | File Templates.
 */
public class ManageToolsView extends Composite implements ManageToolsPresenter.Display {
    interface ManageToolsViewUiBinder extends UiBinder<Widget, ManageToolsView> {
    }

    private static ManageToolsViewUiBinder ourUiBinder = GWT.create(ManageToolsViewUiBinder.class);
    public static ConstantsPresenter constants;

    @UiField
    protected Label toolsCatalog;

    @UiField
    protected Button addToolButton;

    @UiField
    protected Button deleteToolButton;
    @UiField
    protected Button backButton;

    @UiField
    protected Button editToolButton;

    @UiField
    protected ResizeLayoutPanel dataGridContainer;

    protected DataGrid<UICrumbsToolItem> toolTable;

    protected MultiSelectionModel<UICrumbsToolItem> selectionModel;

    TextColumn<UICrumbsToolItem> toolNameColumn;
    TextColumn<UICrumbsToolItem> toolDescriptionColumn;
    TextColumn<UICrumbsToolItem> worksColumn;

    @Override
    public void setData(ArrayList<UICrumbsToolItem> toolItems) {
        ListDataProvider<UICrumbsToolItem> provider = new ListDataProvider<UICrumbsToolItem>(toolItems);
        provider.addDataDisplay(this.toolTable);

        ColumnSortEvent.ListHandler<UICrumbsToolItem> sortHandler = new ColumnSortEvent.ListHandler<UICrumbsToolItem>(provider.getList());
        this.toolTable.addColumnSortHandler(sortHandler);
        if (toolNameColumn != null) {
            sortHandler.setComparator(toolNameColumn, new Comparator<UICrumbsToolItem>() {
                @Override
                public int compare(UICrumbsToolItem o1, UICrumbsToolItem o2) {
                    return o1.getToolName().toLowerCase().compareTo(o2.getToolName().toLowerCase());
                }
            });
        }

        if (toolDescriptionColumn != null) {
            sortHandler.setComparator(toolDescriptionColumn, new Comparator<UICrumbsToolItem>() {
                @Override
                public int compare(UICrumbsToolItem o1, UICrumbsToolItem o2) {
                    return o1.getToolsDescription().toLowerCase().compareTo(o2.getToolsDescription().toLowerCase());
                }
            });
        }

        if (worksColumn != null) {
            sortHandler.setComparator(worksColumn, new Comparator<UICrumbsToolItem>() {
                @Override
                public int compare(UICrumbsToolItem o1, UICrumbsToolItem o2) {
                    return o1.getWorksAsString().toLowerCase().compareTo(o2.getWorksAsString().toLowerCase());
                }
            });
        }

        this.toolTable.getColumnSortList().push(this.toolNameColumn);
        ColumnSortEvent.fire(toolTable, toolTable.getColumnSortList());
    }

    @Override
    public HasClickHandlers getDeleteToolButton() {
        return deleteToolButton;
    }

    @Override
    public HasClickHandlers getEditToolButton() {
        return editToolButton;
    }


    @Override
    public void setEnableDel() {
        deleteToolButton.setEnabled(true);
    }

    @Override
    public void setEnableEdit(boolean a) {
        editToolButton.setEnabled(a);
    }

    @Override
    public void setDisableDel() {
        deleteToolButton.setEnabled(false);
    }


    @Override
    public HasClickHandlers getBackButton() {
        return backButton;
    }

    @Override
    public HasClickHandlers getAddToolButton() {
        return addToolButton;
    }

    @Override
    public Set<UICrumbsToolItem> getSelectedTools() {
        return selectionModel.getSelectedSet();
    }

    @Override
    public DataGrid<UICrumbsToolItem> getTable() {
        return toolTable;
    }

    @Override
    public Widget asWidget() {
        return this;
    }

    @Override
    public MultiSelectionModel<UICrumbsToolItem> getSelectionModel() {
        return this.selectionModel;
    }

    @Override
    public void setSelectionModel(MultiSelectionModel<UICrumbsToolItem> selectionModel) {
        this.toolTable.setSelectionModel(selectionModel);
    }

    @Override
    public void setDoubleHandler(DoubleClickHandler handler) {
        //To change body of implemented methods use File | Settings | File Templates.
    }


    public ManageToolsView() {
        constants = GWT.create(ConstantsPresenter.class);

        HTMLPanel rootElement = (HTMLPanel) (ourUiBinder.createAndBindUi(this));

        toolsCatalog.addStyleName(Recources.INSTANCE.styles().titleCatalog());
        toolsCatalog.setText(constants.TOOLS_CATALOG());
        addToolButton.setStyleName(Recources.INSTANCE.styles().Caption());
        deleteToolButton.setStyleName(Recources.INSTANCE.styles().Caption());
        backButton.setStyleName(Recources.INSTANCE.styles().Caption());
        editToolButton.setStyleName(Recources.INSTANCE.styles().Caption());

        initWidget(rootElement);
        deleteToolButton.setEnabled(false);
        editToolButton.setEnabled(false);
        this.toolTable = new DataGrid<UICrumbsToolItem>(999);
        this.dataGridContainer.add(this.toolTable);
        dataGridContainer.setStyleName(Recources.INSTANCE.styles().ShowCatalogTable());
        //toolTable = new CellTable<UICrumbsToolItem>();
        /* toolTable.setSelectionModel(new MultiSelectionModel<UICrumbsToolItem>(),
        DefaultSelectionEventManager.<UICrumbsToolItem>createCheckboxManager());
        */
        /*
      final MultiSelectionModel<ContactInfo> selectionModel = new MultiSelectionModel<ContactInfo>(
     ContactDatabase.ContactInfo.KEY_PROVIDER);
 cellTable.setSelectionModel(selectionModel,
     DefaultSelectionEventManager.<ContactInfo> createCheckboxManager());

         Column<ContactInfo, Boolean> checkColumn = new Column<ContactInfo, Boolean>(
                 new CheckboxCell(true, false)) {
             @Override
             public Boolean getValue(ContactInfo object) {
                 // Get the value from the selection model.
                 return selectionModel.isSelected(object);
             }
         };
        */
        //   final MultiSelectionModel<UICrumbsToolItem> selectionModel = new MultiSelectionModel<UICrumbsToolItem>();
        this.selectionModel = new MultiSelectionModel<UICrumbsToolItem>();
        //toolTable.setSelectionModel(this.selectionModel, DefaultSelectionEventManager.<UICrumbsToolItem> createCheckboxManager());
        //   toolTable.

        /* Column<UICrumbsToolItem,Boolean> checkColumn = new Column<UICrumbsToolItem, Boolean>(new CheckboxCell(true,false)) {
          @Override
          public Boolean getValue(UICrumbsToolItem uiCrumbsToolItem) {
              return ManageToolsView.this.selectionModel.isSelected(uiCrumbsToolItem);
          }
      };  */

        toolNameColumn = new TextColumn<UICrumbsToolItem>() {
            @Override
            public String getValue(UICrumbsToolItem uiCrumbsToolItem) {
                return uiCrumbsToolItem.getToolName();
            }
        };
        toolNameColumn.setSortable(true);
        toolDescriptionColumn = new TextColumn<UICrumbsToolItem>() {
            @Override
            public String getValue(UICrumbsToolItem uiCrumbsToolItem) {
                return uiCrumbsToolItem.getToolsDescription();
            }
        };
        toolDescriptionColumn.setSortable(true);
        worksColumn = new TextColumn<UICrumbsToolItem>() {
            @Override
            public String getValue(UICrumbsToolItem uiCrumbsToolItem) {
                return uiCrumbsToolItem.getWorksAsString();
            }
        };
        worksColumn.setSortable(true);


        //toolTable.addColumn(checkColumn);
        toolTable.addColumn(toolNameColumn, SafeHtmlUtils.fromString("Tool name"));
        toolTable.addColumn(toolDescriptionColumn, SafeHtmlUtils.fromString("Tool description"));
        toolTable.addColumn(worksColumn, SafeHtmlUtils.fromString("List of works"));
        toolTable.setPageSize(9999999);
    }


}